/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.diplomka.datalayer.daoimpl;

import cz.muni.fi.diplomka.datalayer.dao.BuildingInProgressDao;
import cz.muni.fi.diplomka.datalayer.entity.BuildingInProgressEntity;
import cz.fi.muni.diplomka.common.Coordinates;
import cz.fi.muni.diplomka.common.enumeration.BuildingType;
import cz.muni.fi.diplomka.datalayer.util.DbConnector;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Jakub Kutil
 */
public class BuildingInProgressDaoImpl implements BuildingInProgressDao {

    private Connection con = null;
    private PreparedStatement ps = null;
    private ResultSet rs = null;

    public BuildingInProgressDaoImpl() {
    }

    @Override
    public void addBuildingInProgress(BuildingInProgressEntity buildingInProgressEntity) {
        if (buildingInProgressEntity == null) {
            throw new IllegalArgumentException("BuildingInProgressEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String insert = "INSERT INTO building_in_progress"
                    + "(city_id, x_coordinate, y_coordinate, time_of_finish, name) VALUES"
                    + "(?,?,?,?,?)";
            ps = con.prepareStatement(insert);


            ps.setLong(1, buildingInProgressEntity.getCityId());
            ps.setInt(2, buildingInProgressEntity.getCoordinates().getX_coordinate());
            ps.setInt(3, buildingInProgressEntity.getCoordinates().getY_coordinate());
            ps.setTimestamp(4, buildingInProgressEntity.getTimeOfFinish());
            ps.setString(5, buildingInProgressEntity.getName().toString());

            ps.executeUpdate();

        } catch (SQLException ex) {
            Logger.getLogger(BuildingInProgressDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void editBuildingInProgress(BuildingInProgressEntity buildingInProgressEntity) {
        if (buildingInProgressEntity == null) {
            throw new IllegalArgumentException("BuildingInProgressEntity can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String update = "UPDATE building_in_progress SET time_of_finish=?, name=? WHERE  city_id=? AND x_coordinate=? AND y_coordinate=?";

            ps = con.prepareStatement(update);

            ps.setTimestamp(1, buildingInProgressEntity.getTimeOfFinish());
            ps.setString(2, buildingInProgressEntity.getName().toString());
            ps.setLong(3, buildingInProgressEntity.getCityId());
            ps.setInt(4, buildingInProgressEntity.getCoordinates().getX_coordinate());
            ps.setInt(5, buildingInProgressEntity.getCoordinates().getY_coordinate());

            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(BuildingInProgressDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public void deleteBuildingInProgress(Long id, Coordinates coordinates) {
        if (id == null) {
            throw new IllegalArgumentException("ID can not be null");
        }
        if (coordinates == null) {
            throw new IllegalArgumentException("ID can not be null");
        }
        try {
            con = DbConnector.getDBConnection();
            String delete = "DELETE FROM building_in_progress WHERE city_id=? AND x_coordinate=? AND y_coordinate=?";

            ps = con.prepareStatement(delete);

            ps.setLong(1, id);
            ps.setInt(2, coordinates.getX_coordinate());
            ps.setInt(3, coordinates.getY_coordinate());

            ps.executeUpdate();
        } catch (SQLException ex) {
            Logger.getLogger(BuildingInProgressDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
    }

    @Override
    public BuildingInProgressEntity getBuildingInProgressAtPosition(Long cityId, Coordinates coordinates) {
        if (cityId == null) {
            throw new IllegalArgumentException("CityID can not be null");
        }
        if (coordinates == null) {
            throw new IllegalArgumentException("Coordinates can not be null");
        }
        BuildingInProgressEntity buildingInProgressEntity = new BuildingInProgressEntity();
        try {
            con = DbConnector.getDBConnection();
            String findBuildingInProgress = "SELECT * FROM building_in_progress WHERE city_id=? AND x_coordinate=? AND y_coordinate=?";

            ps = con.prepareStatement(findBuildingInProgress);

            ps.setLong(1, cityId);
            ps.setInt(2, coordinates.getX_coordinate());
            ps.setInt(3, coordinates.getY_coordinate());

            rs = ps.executeQuery();
            while (rs.next()) {
                buildingInProgressEntity.setCityId(rs.getLong("city_id"));
                Coordinates c = new Coordinates();
                c.setX_coordinate(rs.getInt("x_coordinate"));
                c.setY_coordinate(rs.getInt("y_coordinate"));
                buildingInProgressEntity.setTimeOfFinish(rs.getTimestamp("time_of_finish"));
                buildingInProgressEntity.setName(BuildingType.valueOf(rs.getString("name")));
            }

        } catch (SQLException ex) {
            Logger.getLogger(BuildingInProgressDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return buildingInProgressEntity;
    }

    @Override
    public List<BuildingInProgressEntity> getAllBuildingsInProgressForCity(Long cityId) {
        if (cityId == null) {
            throw new IllegalArgumentException("CityID can not be null");
        }

        List<BuildingInProgressEntity> getAllBuildingInProgressList = new ArrayList<>();

        try {
            con = DbConnector.getDBConnection();
            String getAllBuildingInProgresssOfCity = "SELECT * FROM building_in_progress WHERE city_id=?";

            ps = con.prepareStatement(getAllBuildingInProgresssOfCity);
            ps.setLong(1, cityId);

            rs = ps.executeQuery();

            while (rs.next()) {
                BuildingInProgressEntity buildingInProgressEntity = new BuildingInProgressEntity();
                buildingInProgressEntity.setCityId(rs.getLong("city_id"));
                Coordinates c = new Coordinates();
                c.setX_coordinate(rs.getInt("x_coordinate"));
                c.setY_coordinate(rs.getInt("y_coordinate"));
                buildingInProgressEntity.setCoordinates(c);
                buildingInProgressEntity.setTimeOfFinish(rs.getTimestamp("time_of_finish"));
                buildingInProgressEntity.setName(BuildingType.valueOf(rs.getString("name")));

                getAllBuildingInProgressList.add(buildingInProgressEntity);

            }
        } catch (SQLException ex) {
            Logger.getLogger(BuildingInProgressDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return getAllBuildingInProgressList;
    }

    @Override
    public List<BuildingInProgressEntity> getAllBuildingsInProgress() {

        List<BuildingInProgressEntity> getAllBuildingInProgressList = new ArrayList<>();

        try {
            con = DbConnector.getDBConnection();
            String getAllBuildingInProgresss = "SELECT * FROM building_in_progress";

            ps = con.prepareStatement(getAllBuildingInProgresss);


            rs = ps.executeQuery();

            while (rs.next()) {
                BuildingInProgressEntity buildingInProgressEntity = new BuildingInProgressEntity();
                buildingInProgressEntity.setCityId(rs.getLong("city_id"));
                Coordinates c = new Coordinates();
                c.setX_coordinate(rs.getInt("x_coordinate"));
                c.setY_coordinate(rs.getInt("y_coordinate"));
                buildingInProgressEntity.setCoordinates(c);
                buildingInProgressEntity.setTimeOfFinish(rs.getTimestamp("time_of_finish"));
                buildingInProgressEntity.setName(BuildingType.valueOf(rs.getString("name")));


                getAllBuildingInProgressList.add(buildingInProgressEntity);

            }
        } catch (SQLException ex) {
            Logger.getLogger(BuildingInProgressDaoImpl.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            DbConnector.closeConnection(rs, ps, con);
        }
        return getAllBuildingInProgressList;
    }
}
